package hms.rooms;

import java.util.Calendar;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;

import hms.database.DAO;
import hms.database.DAOnoGUI;
import hms.database.DBRowList;
import hms.reservations.Reservation;
import hms.reservations.ReservationView;

import javax.swing.ListModel;
import javax.swing.event.ListDataListener;

public class RoomTable extends DBRowList<Room> implements ListModel{
	
	public RoomTable(){
		this.fillFromDatabase();
	}
	
	@Override
	public void fillFromDatabase() {
		fillFromDatabase(Room.class);
	}

	/**
	 * @return the number of rooms in the database
	 */
	public int getNumberOfRooms() {
		return this.toArray().length;
	}
	
	/**
	 * Look up Room by Room Label. Returns null if no room has that label or database is empty
	 */
	public Room getRoomByLabel(String i_roomLabel){

		Room newRoom = null;
		
		for(Room test : this){
			if(test.getRoomLabel().equalsIgnoreCase(i_roomLabel)){
				newRoom = test;
				break;
			}
		}
		return newRoom;
	}
	
	public Room addRoom(String roomLabel, String roomTypeLabel,
			String occupationStatus, int floor){
		Room newRoom = new Room();
		newRoom.setRoomLabel(roomLabel);
		newRoom.setRoomTypeLabel(roomTypeLabel);
		newRoom.setOccupationStatus(occupationStatus);
		newRoom.setFloorNumber(floor);
		newRoom.setAdditionalNotes(null);
		newRoom.setDecommissionDateComplete(null);
		newRoom.setDecommissionReason(null);
		newRoom.setReservationID(null);
		newRoom.setConnectedRoomLabel(null);
		this.add(newRoom);
		return newRoom;
	}

	private int indexOfID(int roomID) {
		int i = 0;
		for(Room r : this) {
			if(r.getUniqueIdentifier()==roomID) {
				return i;
			}
			i++;
		}
		return -1;
	}
	
	public void filterAvailableRoomsDuring(Calendar fromDate, Calendar toDate) {
		filterAvailableRoomsDuring(new Date(fromDate.getTimeInMillis()), new Date(toDate.getTimeInMillis()));
	}
	
	
	public void filterAvailableRoomsDuring(Date fromDate, Date toDate) {
		ReservationView rv = new ReservationView();
		rv.fillFromDateRange(fromDate, toDate);
		DAO localDAO = DAOnoGUI.getDAO();

		for(Reservation r : rv) {
			ResultSet rs = localDAO.getResult("SELECT ROOMID FROM ROOM_RESERVATION WHERE RESERVATION_ID="+r.getUniqueIdentifier()+";");
			try {
				while(rs.next()) {
					int excludeID = rs.getInt("ROOMID");
					int index = indexOfID(excludeID);
					if(index>=0) {
						remove(index);
					}
				}
			} catch (SQLException e) {
			}
		}
		
		DAOnoGUI.closeDAO();
		
	}
	
	public void filterReservedRoomsDuring(Date fromDate, Date toDate) {
		ReservationView rv = new ReservationView();
		rv.fillFromDateRange(fromDate, toDate);
		DAO localDAO = DAOnoGUI.getDAO();

		for(Reservation r : rv) {
			ResultSet rs = localDAO.getResult("SELECT ROOMID FROM ROOM_RESERVATION WHERE RESERVATION_ID="+r.getUniqueIdentifier()+";");
			try {
				while(rs.next()) {
					int excludeID = rs.getInt("ROOMID");
					int index = indexOfID(excludeID);
					if(index>=0) {
						remove(index);
					}
				}
			} catch (SQLException e) {
			}
		}
		
		DAOnoGUI.closeDAO();
		
	}

	@Override
	public void addListDataListener(ListDataListener arg0) {
		
	}
	
	@Override
	public Object getElementAt(int arg0) {
		return get(arg0);
	}
	
	@Override
	public int getSize() {
		return size();
	}
	
	@Override
	public void removeListDataListener(ListDataListener arg0) {
		
	}
}
